import puppeteer from "puppeteer-core";
import path from "node:path";
import {spawn} from "child_process"
const argv = process.argv;
const te = argv[2];
// 1、创建浏览器 关闭无头模式
const browser = await puppeteer.launch({
    executablePath: path.resolve('C:/Program Files/Google/Chrome/Application/chrome.exe'),
    headless:false
});
// 2、创建页面
const page = await browser.newPage();
await page.setViewport({width:1920,height:1000});
// 3、跳转页面
await page.goto("https://www.juejin.cn");
// 4、等待标签出现后 
await page.waitForSelector(".side-navigator-wrap")
// $ querySelector、$$ querySelectorAll
const elements = await page.$$(".side-navigator-wrap .nav-item-wrap span")
const collectFn = async ()=>{
    const titleList = []
    await page.waitForSelector(".entry-list");
    const elements = await page.$$(".entry-list .title-row a")
    for await (const el of elements){
        const prop = await el.getProperty("innerText");
        const text = await prop.jsonValue();
        titleList.push(text);
    }
    const pyProcess = spawn("python",["wordcloud.py",titleList.join(",")]);
    pyProcess.stdout.on("data",data => {
    })
    pyProcess.stdout.on("error",data => {
    })
    pyProcess.stdout.on("close",data => {
    })
}
for await (const el of elements){
    const prop = await el.getProperty("innerText");
    const text = await prop.jsonValue();
    console.log(text)
    if (text === (te || "前端")) {
        await el.click();
        collectFn()
        break;
    }
}